from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

#设置连接数据库的URL
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:mysql@127.0.0.1:3306/tuyu'

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
#查询时会显示原始SQL语句
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)


class Teacher(object):
    """教师表"""
    #  定义表名
    __tablename__ = 'teachers'
    # 定义列对象
    tid = db.Column(db.Integer, primary_key=True)
    tname = db.Column(db.String(64), unique=True) # 教师姓名
    gender = db.Column(db.Enum("MAN", "WOMEN"), default="MAN") # 性别
    mobile = db.Column(db.String(11), unique=True, nullable=False) # 手机号
    address = db.Column(db.String(64), unique=True) # 地址
    school = db.Column(db.String(64), unique=True) # 毕业学校
    grade = db.Column(db.String(32), db.ForeignKey('grades.id')) # TODO 年级外键
    tscore = db.Column(db.Integer, nullable=False) # 高考成绩
    experience = db.Column(db.String(64), nullable=False) # 教学经验
    in_job = db.Column(db.Integer, db.ForeignKey("in_do.id")) # TODO 是否在职
    subject = db.Column(db.Integer, db.ForeignKey("subjects.pid")) # TODO 学科
    avatar_url = db.Column(db.String(256)) # 教师头像路径


    def to_teacher(self):
        resp_dict = {
            "tid": self.tid,
            "tname": self.tname,
            "gender": self.gender if self.gender else "MAN",
            "mobile": self.mobile,
            "address": self.address,
            "school" : self.school,
            "grade": self.grade,
            "tscore": self.tscore,
            "experience": self.experience,
            "in_job": self.in_job,
            "subject": self.subject,
            "avatar_url": self.avatar_url # TODO 图片的地址
        }

        return resp_dict


class Student(object):
    """学生表"""
    #  定义表名
    __tablename__ = 'students'
    # 定义列对象
    sid = db.Column(db.Integer, primary_key=True) # 学生
    name = db.Column(db.String(64), unique=True)  # 学生姓名
    gender = db.Column(db.Enum("MAN", "WOMEN"), default="MAN")  # 性别
    address = db.Column(db.String(64), unique=True)  # 学生地址
    character = db.Column(db.String(64), unique=True) # 性格
    subject = db.Column(db.Integer, db.ForeignKey("subjects.pid"))  # TODO 学科
    grade = db.Column(db.String(32), db.ForeignKey('grades.id'))  # TODO 年级外键
    score = db.Column(db.Integer, nullable=False)  # 提升分数
    s_num = db.Column(db.Integer, nullable=False) # 上课次数
    avatar_url = db.Column(db.String(256))  # 学生照片路径
    in_read = db.Column(db.Integer, db.ForeignKey("in_do.id")) # TODO 是否在读
    parents = db.Column(db.String(32), nullable=True) # 家长姓名
    mobile = db.Column(db.String(11), unique=True, nullable=False)  # 家长手机号
    teacher = db.Column(db.Integer, db.ForeignKey("teachers.tid")) # 老师

    def to_teacher(self):

        resp_dict = {
            "sid": self.sid,
            "name": self.name,
            "gender": self.gender if self.gender else "MAN",
            "character": self.character,
            "subject": self.subject,
            "grade": self.grade,
            "score": self.score,
            "s_num": self.s_num,
            "avatar_url": self.avatar_url,  # TODO 图片的地址
            "in_read": self.in_read,
            "parents": self.parents,
            "mobile": self.mobile,

        }
        return resp_dict


class In_Do(object):
    """是否在职表"""
    __tablename__ = 'in_do'
    id = db.Column(db.Integer, primary_key=True)
    in_set = db.Column(db.String(32), nullable=False)


class Subject(object):
    """学科表"""
    __tablename__ = 'subjects'
    pid = db.Column(db.Integer, primary_key=True)
    s_name = db.Column(db.String(32), nullable=False) # 学科名
    category = db.Column(db.Integer, db.ForeignKey("s_category.id"), nullable=False) # 学科类别

    def to_subject(self):
        resp_dict = {
            "s_name": self.s_name,
            "category": self.category
        }
        return resp_dict


class Subject_Category(object):
    """学科类表"""
    __tablename__ = "s_category"
    id = db.Column(db.Integer, primary_key=True)
    course = db.Column(db.String(32), nullable=False) # 学科类 文化类  艺术类


class Grade(object):
    """年级表"""
    __tablename__ = 'grades'
    id = db.Column(db.Integer, primary_key=True) # 年级id
    grade = db.Column(db.String(64), nullable=False) # 年级
    context = db.Column(db.String(), nullable=False) # 教学内容
    pid = db.Column(db.Integer, db.ForeignKey("subjects.pid"))